package com.asia5b.a5mall.middleware.logic.viewmodel.member

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.api.ApiManager
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.bean.CommentMultiItemEntity
import com.asia5b.a5mall.middleware.utils.judge
import io.reactivex.functions.Consumer

class CommentListFragmentVM :SimpleViewModel() {
   private var page = 1
   private var sumPage = 1000

    /**
     * 待评价订单 1
     * http://47.88.223.5:4001/trade/orders?page_no=1&page_size=10&order_status=WAIT_COMMENT
     * //待追评 2
     * http://47.88.223.5:4001/members/comments/list?page_no=1&page_size=10&comment_status=WAIT_CHASE
     * 已评价 3
     * http://47.88.223.5:4001/members/comments/list?page_no=1&page_size=10&comment_status=FINISHED
     * @Note   加载评论列表
     * @param  page  分页
     * @param  state 状态 1：待评价订单,2:待追评,3:已评价
     */

    val getDatasResultLiveData=MutableLiveData<ResultModel<ArrayList<CommentMultiItemEntity>>>()
    fun getDatas(refresh: Int, state: Int) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
           dismissLoadingBar()
            return
        }


        if (state == 1) {
            val observable = orderApi.getOrderList(null,"WAIT_COMMENT", null,page)
            baseSubscribe(observable, SimpleNetParams(isShowLoading = (refresh == 0)), Consumer { resultModel ->
                sumPage = resultModel.sumPage
                page = resultModel.page + 1//预备加载下一页
                val list= arrayListOf<CommentMultiItemEntity>()
                resultModel.data?.run {
                    for (orderDetailModel in this) {
                        orderDetailModel.type = state
                        list.add(orderDetailModel)
                    }
                }
//                providerView().getDatasResult(list)
                getDatasResultLiveData.value=ResultModel<ArrayList<CommentMultiItemEntity>>().apply {
                    this.sumPage=resultModel.sumPage
                    this.totalData=resultModel.totalData
                    this.page=resultModel.page
                    this.data=list
                }
            })
        } else {
            val comment_status = (state == 2).judge("WAIT_CHASE", "FINISHED")
            val observable = memberApi.getCommentsList(page, ApiManager.PAGE_SIZE, comment_status)
            baseSubscribe(observable, SimpleNetParams(isShowLoading = (refresh == 0)), Consumer { resultModel ->
                sumPage = resultModel.sumPage
                page = resultModel.page + 1//预备加载下一页
                val list= arrayListOf<CommentMultiItemEntity>()
                resultModel.data?.run {
                    for (orderDetailModel in this) {
                        orderDetailModel.type = state
                        list.add(orderDetailModel)
                    }
                }
//                providerView().getDatasResult(list)
                getDatasResultLiveData.value=ResultModel<ArrayList<CommentMultiItemEntity>>().apply {
                    this.sumPage=resultModel.sumPage
                    this.totalData=resultModel.totalData
                    this.page=resultModel.page
                    this.data=list
                }
            })
        }
    }
}